package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import entity.tDevice;
import entity.tUser;
import entity.tUserAndtDevice;

import service.tDeviceService;
import service.tUserAndtDeviceService;

public class AddtDeviceAction {
	
	private String userId;
	private String deviceNo;
	private String deviceName;
	private String deviceArea;
	private String place;
	private String deviceDescribe;
	private Timestamp addTime;
	private String mac;
	private int ph;
	private int orp;
	private int temperature;
	private int ec;
	private int openEC;
	private int closeEC;
	private tUser tuser;
	private String[] list;
	private tDeviceService tdeviceService;
	private tUserAndtDeviceService uadservice;
	private String result;

	
	@SuppressWarnings("unchecked")
	public String add() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		
		request.getAttribute("userId");
		
		System.out.println(addTime);
		tDevice tdevice = new tDevice();
		Date date=new Date();
		Timestamp ts = new Timestamp(date.getTime());
		
		System.out.println(deviceNo+"    "+deviceName+"    "+mac);
		
		if (deviceNo == null || deviceNo.equals(null) || deviceNo=="" || deviceNo.equals("")){
			result = "设备序列号为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		tdevice.setDeviceNo(deviceNo);
		
		if (deviceName == null || deviceName.equals(null) || deviceName=="" || deviceName.equals("")){
			result = "设备名称为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		tdevice.setDeviceName(deviceName);
		
		if (mac == null || mac.equals(null) || mac=="" || mac.equals("")){
			result = "MAC地址为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		tdevice.setMac(mac);
		
		if (addTime == null){
			tdevice.setAddTime(ts);
			//return null;
		}
		if (addTime != null){
			tdevice.setAddTime(addTime);
		}
		
		tdevice.setDeviceArea(deviceArea);
		tdevice.setPlace(place);
		
		tdevice.setPh(0);
		tdevice.setOrp(0);
		tdevice.setTemperature(0);
		tdevice.setEc(0);
		tdevice.setOpenEC(0);
		tdevice.setCloseEC(0);
		
		tdevice.setEsil(0);
		tdevice.setEc1(0);
		tdevice.setEc2(0);
		tdevice.setEc3(0);
		tdevice.setEc4(0);
		tdevice.setEc5(0);
		tdevice.setEc6(0);
		tdevice.setEc7(0);
		tdevice.setEc8(0);
		tdevice.setEc9(0);
		tdevice.setEc10(0);
		tdevice.setEc11(0);
		tdevice.setEc12(0);
		tdevice.setEc13(0);
		
		System.out.println("list:"+list);
		if (list != null){
			for (int i = 0; i < list.length; i++) {
				String shuiguoname=list[i];
				if (shuiguoname.equals("ph")){
					tdevice.setPh(1);
				}
				
				if (shuiguoname.equals("orp")){
					tdevice.setOrp(1);
				}
				
				if (shuiguoname.equals("temperature")){
					tdevice.setTemperature(1);
				}
				
				if (shuiguoname.equals("ec")){
					tdevice.setEc(1);
				}
				
				if (shuiguoname.equals("openEC")){
					tdevice.setOpenEC(1);
				}
				
				if (shuiguoname.equals("closeEC")){
					tdevice.setCloseEC(1);
				}
				
				if (shuiguoname.equals("esil")){
					tdevice.setEsil(1);
				}
				if (shuiguoname.equals("ec1")){
					tdevice.setEc1(1);
				}
				if (shuiguoname.equals("ec2")){
					tdevice.setEc2(1);
				}
				if (shuiguoname.equals("ec3")){
					tdevice.setEc3(1);
				}
				if (shuiguoname.equals("ec4")){
					tdevice.setEc4(1);
				}
				if (shuiguoname.equals("ec5")){
					tdevice.setEc5(1);
				}
				if (shuiguoname.equals("ec6")){
					tdevice.setEc6(1);
				}
				if (shuiguoname.equals("ec7")){
					tdevice.setEc7(1);
				}
				if (shuiguoname.equals("ec8")){
					tdevice.setEc8(1);
				}
				if (shuiguoname.equals("ec9")){
					tdevice.setEc9(1);
				}
				if (shuiguoname.equals("ec10")){
					tdevice.setEc10(1);
				}
				if (shuiguoname.equals("ec11")){
					tdevice.setEc11(1);
				}
				if (shuiguoname.equals("ec12")){
					tdevice.setEc12(1);
				}
				if (shuiguoname.equals("ec13")){
					tdevice.setEc13(1);
				}
				System.out.println(shuiguoname);
			}
		}
		
		
		
		tdevice.setDistributeTime(ts);
		tdevice.setDeviceDescribe(deviceDescribe);
		tdevice.setIndustry("");
		tdevice.setSketchMap("null");
		tdevice.setVideoUrl("null");
		
		
		tdeviceService.add(tdevice);
		
		if (userId != null){
			
			System.out.println(mac);
			
			List<tDevice> device = tdeviceService.searchBymac(mac);
		
			String deviceId = device.get(0).getDeviceId();
			
			System.out.println("deviceId:"+deviceId);
			System.out.println("userId:"+userId);
			tUserAndtDevice tuserandtdevice = new tUserAndtDevice();
			
			tuserandtdevice.setUserId(userId);
			tuserandtdevice.setDeviceId(deviceId);
			
			uadservice.add(tuserandtdevice);
 		
			return "success";
		}
		
		return "success";
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}



	public String getDeviceNo() {
		return deviceNo;
	}

	public void setDeviceNo(String deviceNo) {
		this.deviceNo = deviceNo;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public String getDeviceArea() {
		return deviceArea;
	}

	public void setDeviceArea(String deviceArea) {
		this.deviceArea = deviceArea;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getDeviceDescribe() {
		return deviceDescribe;
	}

	public void setDeviceDescribe(String deviceDescribe) {
		this.deviceDescribe = deviceDescribe;
	}

	public Timestamp getAddTime() {
		return addTime;
	}

	public void setAddTime(Timestamp addTime) {
		this.addTime = addTime;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public int getPh() {
		return ph;
	}

	public void setPh(int ph) {
		this.ph = ph;
	}

	public int getOrp() {
		return orp;
	}

	public void setOrp(int orp) {
		this.orp = orp;
	}

	public int getTemperature() {
		return temperature;
	}

	public void setTemperature(int temperature) {
		this.temperature = temperature;
	}

	public int getEc() {
		return ec;
	}

	public void setEc(int ec) {
		this.ec = ec;
	}

	public int getOpenEC() {
		return openEC;
	}

	public void setOpenEC(int openEC) {
		this.openEC = openEC;
	}

	public int getCloseEC() {
		return closeEC;
	}

	public void setCloseEC(int closeEC) {
		this.closeEC = closeEC;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public tUser getTuser() {
		return tuser;
	}

	public void setTuser(tUser tuser) {
		this.tuser = tuser;
	}

	public String[] getList() {
		return list;
	}

	public void setList(String[] list) {
		this.list = list;
	}

	public tUserAndtDeviceService getUadservice() {
		return uadservice;
	}

	public void setUadservice(tUserAndtDeviceService uadservice) {
		this.uadservice = uadservice;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	


}
